//
//  MAThreeImgsCell.m
//  meiang_edu
//
//  Created by hfcb on 2018/7/31.
//  Copyright © 2018年 com.meiang.meiang_edu. All rights reserved.
//



#import "MAThreeImgsCell.h"
#import "MAHomeModel.h"

#define ThreeImgsCell @"ThreeImgsCell"

@interface MAThreeImgsCell ()<UIAlertViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *containerView;
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *detailLabel;
@property (weak, nonatomic) IBOutlet UILabel *contentLabel;
@property (weak, nonatomic) IBOutlet UIImageView *img1;
@property (weak, nonatomic) IBOutlet UIImageView *img2;
@property (weak, nonatomic) IBOutlet UIImageView *img3;
@property (weak, nonatomic) IBOutlet UILabel *dateLabel;
@property (weak, nonatomic) IBOutlet UIImageView *typeIcon;
@property (weak, nonatomic) IBOutlet UILabel *cellTypeLabel;

@property (weak, nonatomic) IBOutlet UILabel *titleLabel;

/** tableView */
@property(nonatomic,weak) UITableView *tableView;



@end

@implementation MAThreeImgsCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.containerView.layer.cornerRadius = 4;
    self.containerView.layer.masksToBounds = YES;
    self.iconView.layer.cornerRadius = 20;
    self.iconView.layer.masksToBounds = YES;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil] lastObject];
    }
    return self;
}

+ (instancetype)cellWithTableView:(UITableView *)tableView {
    
    MAThreeImgsCell *cell = [tableView dequeueReusableCellWithIdentifier:ThreeImgsCell];
    if (cell == nil) {
        cell = [[MAThreeImgsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ThreeImgsCell];
    }
    cell.tableView = tableView;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (IBAction)delBtnDidClick {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"友情提示" message:@"确定要删除这条消息吗?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alert show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        DLog(@"shanchu");
        [self deleteData];
    }
}

- (void)deleteData {
    
    // 删除数据
    MAUserInfo *userinfo = [MAUserInfoTool userinfo];
    //    if (!userinfo.token) {
    //        [MBManager showBriefAlert:@"登录状态已过期,请重新登录"];
    //        return;
    //    }
    [MBManager showLoading];
    NSDictionary *body = @{@"userkey" : userinfo.ID, @"msgkey" : self.model.keyid};
    
    NSError *error;
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:body options:0 error:&error];
    
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    
    NSMutableURLRequest *req = [[AFJSONRequestSerializer serializer] requestWithMethod:@"POST" URLString:MESSAGE_DELETE parameters:nil error:nil];
    
    [req setValue:@"application/text" forHTTPHeaderField:@"Content-Type"];
    [req setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [req setValue:@"token" forHTTPHeaderField:userinfo.token];
    
    [req setHTTPBody:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
    
    [[manager dataTaskWithRequest:req completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
        [MBManager hideAlert];
        if (!error) {
            NSInteger msgcode = [responseObject[@"msgcode"] integerValue];
            NSString *message = responseObject[@"message"];
            if (msgcode == 1) {
                [MBManager showBriefAlert:message];
                [self.tableView reloadData];
            } else {
                [MBManager showBriefAlert:message];
            }
            DLog(@"Reply JSON: %@", responseObject);
            
        } else {
            DLog(@"Error: %@, %@, %@", error, response, responseObject);
            [MBManager showBriefAlert:@"网络错误"];
        }
    }] resume];
    
}

#pragma mark - getter & setter
- (void)setModel:(MAHomeModel *)model {
    _model = model;
    [self.iconView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",IMG_OSS,model.sender_img]] placeholderImage:[UIImage imageNamed:@"homepage_navigationbar_picture"]];
    self.nameLabel.text = model.sender_name;
    self.detailLabel.text = [NSString stringWithFormat:@"发送人 : %@",model.sender_name];
    self.titleLabel.text = model.message_title;
    self.contentLabel.text = model.message_content;
    NSArray *imgs = [model.message_images componentsSeparatedByString:@","];
    [self.img1 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",IMG_OSS,imgs[0]]]];
    [self.img2 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",IMG_OSS,imgs[1]]]];
    if (imgs.count > 2) {
        [self.img3 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",IMG_OSS,imgs[2]]]];
    }
    self.dateLabel.text = model.send_time;
    
    if (model.functionimage.length) {
        [self.typeIcon sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",IMG_OSS,model.functionimage]]];
    }
    self.cellTypeLabel.text = model.message_typechild;
    
    CGFloat threeLineH = [@"\n\n" sizeWithLabelWidth:SCREEN_WIDTH - 32 font:Font(FONT_SIZE_14)].height;
    CGFloat labelH =  [model.message_content sizeWithLabelWidth:SCREEN_WIDTH - 32 font:Font(FONT_SIZE_14)].height > threeLineH ? threeLineH :  [model.message_content sizeWithLabelWidth:SCREEN_WIDTH - 32 font:Font(FONT_SIZE_14)].height;
    
    CGFloat imgH = (SCREEN_WIDTH - 32 - 4) / 3 * 68 / 108;
    
    model.cellHeight = 142.0 + labelH + imgH;
}
@end
